

local function child_fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
	inst.entity:AddNetwork()
    inst.entity:SetPristine()
    if not TheWorld.ismastersim then
        return inst
    end
    inst:DoTaskInTime(0,function()
        if inst.Ready ~= true then
            inst:Remove()
        end
    end)
    return inst
end


local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
	inst.entity:AddNetwork()

    inst:AddTag("FX")

    inst.entity:SetPristine()
    if not TheWorld.ismastersim then
        return inst
    end
    inst:AddComponent("npc_base_lib")
    -- inst:AddComponent("npc_everything_data")

    inst:DoTaskInTime(0,function()
        if inst.Ready ~= true then
            inst:Remove()
            return
        end

        local points = inst.npc_base_lib:GetSurroundPoints({
            target = Vector3(0,0,0),            
            range = 8,
            num = 4*3,
        })
        inst._point_insts = {}
        for k, pt in pairs(points) do
            local temp = inst:SpawnChild("npc_item_butterflies_cycle_child")
            temp.Ready = true
            temp.Transform:SetPosition(pt.x,0,pt.z)
            table.insert(inst._point_insts,temp)
        end        
        inst.base_inst_flag = 1
        inst._tar_insts = {inst._point_insts[1],inst._point_insts[4],inst._point_insts[8]}
    end)
    inst:DoPeriodicTask(0.4,function()
        local max_num = #inst._point_insts
        inst.base_inst_flag = inst.base_inst_flag + 1
        if inst.base_inst_flag > max_num then
            inst.base_inst_flag = inst.base_inst_flag - max_num 
        end
        local num_1 = inst.base_inst_flag
        local num_2 = num_1 + 4
        if num_2 > max_num then
            num_2 = num_2 - max_num
        end
        local num_3 = num_2 + 4
        if num_3 > max_num then
            num_3 = num_3 - max_num
        end

        inst._tar_insts[1] = inst._point_insts[num_1]
        inst._tar_insts[2] = inst._point_insts[num_2]
        inst._tar_insts[3] = inst._point_insts[num_3]
    end)



    inst.butterflies = {}
    inst:ListenForEvent("Spawn_butterflies",function()            
            if #inst.butterflies == 3 then
                return
            end
            local color = {
                [1] = Vector3(255,0,0),
                [2] = Vector3(0,255,0),
                [3] = Vector3(0,0,255),
            }
            for i = 1, 3, 1 do
                ----------------------------------------------------------------------------------------
                local butterfly = SpawnPrefab("npc_projectile_butterfly")
                butterfly.owner = TheSim:FindFirstEntityWithTag("player")
                butterfly.num = i
                butterfly.current_speed = 4
                butterfly._tar_insts = inst._tar_insts
                butterfly:PushEvent("Set",{
                    pt = Vector3(inst.Transform:GetWorldPosition()),
                    owner = butterfly.owner,
                    speed = 4,
                    target = butterfly._tar_insts[butterfly.num],
                    range = 1,
                    color = color[i]
                })
                butterfly:DoPeriodicTask(0.3,function()    ------ 普通环绕任务
                    if not butterfly:HasTag("attacking") then   
                        local target = butterfly._tar_insts[butterfly.num]
                        local targetpos = Vector3(target.Transform:GetWorldPosition())
                        local x,y,z = butterfly.Transform:GetWorldPosition()
                        -- local dis = inst.npc_base_lib:Distance_Targets(target,butterfly)
                        local dist = VecUtil_Length(targetpos.x - x, targetpos.z - z)
                        butterfly.current_speed = math.min(dist * 8, 10.5) * 0.5
                        butterfly:PushEvent("Set",{
                            target = target,
                            target_fn = function()end,
                            speed = butterfly.current_speed,
                            range = 0.5
                        })
                    end
                end)


                butterfly:SpawnChild("npc_fx_forcefield"):PushEvent("Set",{
                    scale = Vector3(0.6,0.6,0.6),
                    pt = Vector3(0,2,0),
                    color = color[i],
                    light = true,
                })

                table.insert(inst.butterflies,butterfly)    ------ 加入到inst列表里

                ----------------------------------------------------------------------------------------
            end      
    end)

    inst:ListenForEvent("Remove_butterflies",function()
        for k, v in pairs(inst.butterflies) do
            v:Remove()
        end
        inst.butterflies = nil
        inst.butterflies = {}
    end)
    inst:ListenForEvent("attack",function(inst,_table)
        if _table == nil then
            return
        end

        local temp_target_fn = function(butterfly,owner,target)  -------- 为了移除tag 和方便后续处理，套一层
            --------------------------------------------------------------------------------
            --------------------------------------------------------------------------------
            if _table.target_fn then
                _table.target_fn(butterfly,owner,target)
                butterfly:PushEvent("Set",{target_fn = nil})
            end
            --------------------------------------------------------------------------------
            butterfly:RemoveTag("attacking")
            --------------------------------------------------------------------------------
        end

        for k, butterfly in pairs(inst.butterflies) do
            if not butterfly:HasTag("attacking") then
                butterfly:AddTag("attacking")
                butterfly:PushEvent("Set",{
                    target = _table.target,
                    owner = TheSim:FindFirstEntityWithTag("player"),
                    speed = 20,
                    target_fn = temp_target_fn, ---- function (self,owner,target)
                    range = 2,
                })
            end
        end


    end)



    return inst
end

return Prefab("npc_item_butterflies_cycle_child", child_fn),
    Prefab("npc_item_butterflies_cycle", fn)